package com.iemes.controller.warehouse;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.OutreceiptFormMap;
import com.iemes.entity.TransportTransporinfoFormMap;
import com.iemes.mapper.transport.TransporinfoMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.DateUtils;
import com.iemes.util.MyDateUtils;

@RequestMapping("/outreceipt_unback/")
@Controller
public class Outreceipt_UnbacklController extends BaseController {

	@Inject
	private TransporinfoMapper transporinfoMapper;

	// 查看车辆调度明细表，显示按钮
	@RequestMapping("outreceipt_unback")
	public String outreceipt_unback(Model model) {
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/warehouse/outreceipt_unback_list";
	}

	// 车辆调度明细表分页
	@ResponseBody
	@RequestMapping("outreceipt_unback_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception {
		TransportTransporinfoFormMap transporinfoFormMap=getFormMap(TransportTransporinfoFormMap.class);
		transporinfoFormMap.put("formCategory", 1);
		List<Map<String, Object>> transport=transporinfoMapper.getAll(transporinfoFormMap);
		for(Map<String, Object>map:transport){
			map.put("noReturnTime", DateUtils.stringToDateTime(map.get("noReturnTime").toString()));
		}
		LuogicPaging(pageNow, pageSize, transport);
		return pageView;
	}

	// 查看编辑未回单
	@RequestMapping("outreceipt_unback_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		if(Common.isNotEmpty("id")){
			//根据Id获得车辆调度明细表
			TransportTransporinfoFormMap tInfo=transporinfoMapper.getById(id);
			model.addAttribute("tInfo",tInfo);
			//获得相同tInfoId的已回单
			OutreceiptFormMap outreceiptFormMap=new OutreceiptFormMap();
			outreceiptFormMap.put("tInfoId", tInfo.get("id"));
			List<OutreceiptFormMap> receipt=baseMapper.findByNames(outreceiptFormMap);
			model.addAttribute("receipt",receipt);
		}
		return Common.BACKGROUND_PATH + "/warehouse/outreceipt_unback_edit";
	}

	// 修改未回单
	@ResponseBody
	@RequestMapping(value = "outreceipt_unback_editEntity")
	@SystemLog(module = "编辑未回单", methods = "未回单-修改未回单") // 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity(HttpServletRequest request) throws NumberFormatException, Exception {
		TransportTransporinfoFormMap transporinfoFormMap=getFormMap(TransportTransporinfoFormMap.class);
		transporinfoMapper.editEntity(transporinfoFormMap);
		//先删除
		baseMapper.deleteByAttribute("tInfoId","'"+Integer.parseInt(transporinfoFormMap.getStr("id"))+"'", OutreceiptFormMap.class);
		// 新增标签子项
		int tInfoId=Integer.parseInt(request.getParameter("transportTransporinfoFormMap.id"));
		
		//再添加
		String[] material_code=request.getParameterValues("material_code_1");
		String[] productName=request.getParameterValues("productName_1");
		String[] outNumber=request.getParameterValues("outNumber_1");
		if(null!=material_code){
			for(int i=0;i<material_code.length;i++){
				OutreceiptFormMap outreceiptFormMap=new OutreceiptFormMap();
				outreceiptFormMap.put("tInfoId", tInfoId);
				outreceiptFormMap.put("material_code", material_code[i]);
				outreceiptFormMap.put("productName", productName[i]);
				outreceiptFormMap.put("outNumber", outNumber[i]);
				baseMapper.addEntity(outreceiptFormMap);
			}
		}
		return "success";
	}
}